package model

import (
	"ginblog/tools/pagination"
	"ginblog/utils/errmsg"
)

type Comment struct {
	BaseModel
	ArticleId     int    `gorm:"type:int(11);not null;comment:'文章Id'" json:"article_id"`
	ParentId      int    `gorm:"type:int(11);default 0;comment:'父类Id'" json:"parent_id"`
	Content       string `gorm:"type:text(512);comment:'内容'" json:"content"`
	ExamineStatus int    `gorm:"type:int(3);default 0;comment:'审核结果,0:未审核,1:审核通过,2:审核拒绝'" json:"examine_status"`
}

// SelectComment /查询第一级的评论
func (c *Comment) SelectComment(page *pagination.Pagination) {
	var commentList []Comment
	err := Db.Where(&Comment{ArticleId: c.ArticleId, ParentId: 0}).Limit(page.GetPageSize()).Offset(page.GetOffset()).Find(&commentList).Count(&page.Total).Error
	if err != nil {
		c.SteCodeMsg(errmsg.ERROR)
	}
	page.SetTotalPage()
	page.Data = commentList
}

// SelectProbeDownComment /查询第一级下探的评论
func (c *Comment) SelectProbeDownComment(page *pagination.Pagination) {
	var commentList []Comment
	err := Db.Where(&Comment{ArticleId: c.ArticleId, ParentId: c.ParentId}).Limit(page.GetPageSize()).Offset(page.GetOffset()).Find(&commentList).Count(&page.Total).Error
	if err != nil {
		c.SteCodeMsg(errmsg.ERROR)
	}
	page.SetTotalPage()
	page.Data = commentList
}

// AddComment /新增评论
func (c *Comment) AddComment() bool {
	err := Db.Create(&c).Error
	if err != nil {
		return false
	}
	return true
}

// EditComment /编辑评论
func (c *Comment) EditComment() bool {
	err := Db.Model(&c).Where("id = ? ", c.ID).Omit("Id").Updates(c).Error
	if err != nil {
		return false
	}
	return true
}

// DelComment /删除评论
func (c *Comment) DelComment() bool {
	err := Db.Delete(&Comment{}, c.ID).Error
	if err != nil {
		return false
	}
	return true
}
